package edu.bw.dao.impl;

import edu.bw.dao.ProductDao;
import edu.bw.mapper.ProductMapper;
import edu.bw.po.Product;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
@Setter
@Repository
public class ProductDaoImpl implements ProductDao {
//    //交给spring注入
//    @Resource
//    private QueryRunner queryRunner;

//    //用setter注入
//    public void setQueryRunner(QueryRunner queryRunner){
//        this.queryRunner=queryRunner;
//    }


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private ProductMapper productMapper;

//    @Autowired
//    private ConnectionFactory connectionFactory;


    @Override
    public void saveProduct(Product product) {
        jdbcTemplate.update("INSERT INTO spring.product(pname,price,category) VALUES (?,?,?)",
                product.getPname(),product.getPrice(),product.getCategory());
    }

    @Override
    public void updateProduct(Product product) {
        jdbcTemplate.update("UPDATE spring.product SET pname=? ,price=? ,category=? WHERE pid=?",
                product.getPname(),product.getPrice(),product.getCategory(),product.getPid());
    }

    @Override
    public void deleteProduct(Integer pid) {
        jdbcTemplate.update("DELETE FROM spring.product WHERE pid=? ",pid);
    }

    @Override
    public List<Product> findAll() {
        return jdbcTemplate.query("SELECT * FROM spring.product ",productMapper);
    }

    @Override
    public Product findById(int pid) {
        return jdbcTemplate.queryForObject("SELECT * FROM spring.product WHERE pid=?",productMapper,pid);
    }
}
